package main.Q1301_1400;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Q1321_1330 {
    public static void main(String[] args) {
        System.out.println("Question1321：");
        System.out.println("Question1322：");
        System.out.println("Question1323：");
        System.out.println("Question1324：");
        System.out.println("Question1325：删除给定值的叶子节点");
        TreeNode node1 = new TreeNode(1);
        TreeNode node2_1 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node2_2 = new TreeNode(2);
        TreeNode node2_3 = new TreeNode(2);
        TreeNode node4 = new TreeNode(4);
        node1.left = node2_1;
        node1.right = node3;
        node2_1.left = node2_2;
        node3.left = node2_3;
        node3.right = node4;
        System.out.println(new Question1325().removeLeafNodes(node1, 2));
        System.out.println("Question1326：");
        System.out.println("Question1327：");
        System.out.println("Question1328：");
        System.out.println("Question1329：将矩阵按对角线排序");
        System.out.println("Question1330：");
    }
}

class Question1325 {
    public TreeNode process(TreeNode root, int target) {
        if (root.left == null && root.right == null) {//本身为叶子节点
            if (root.val == target) {
                return null;
            } else {
                return root;
            }
        }
        root.left = root.left == null ? null : process(root.left, target);
        root.right = root.right == null ? null : process(root.right, target);
        if (root.left == null && root.right == null) {//删除子节点后该节点为叶子节点
            if (root.val == target) {
                return null;
            } else {
                return root;
            }
        } else {
            return root;
        }
    }

    public TreeNode removeLeafNodes(TreeNode root, int target) {
        return process(root, target);
    }
}

class Question1329 {
    public int[][] diagonalSort(int[][] mat) {
        for (int i = 0; i < mat[0].length; i++) {
            List<Integer> list = new ArrayList<>();
            int row = 0, col = i;
            while (row < mat.length && col < mat[0].length) {
                list.add(mat[row++][col++]);
            }
            Collections.sort(list);
            row = 0;
            col = i;
            for (Integer num : list) {
                mat[row++][col++] = num;
            }
        }
        for (int i = 1; i < mat.length; i++) {
            List<Integer> list = new ArrayList<>();
            int row = i, col = 0;
            while (row < mat.length && col < mat[0].length) {
                list.add(mat[row++][col++]);
            }
            Collections.sort(list);
            row = i;
            col = 0;
            for (Integer num : list) {
                mat[row++][col++] = num;
            }
        }
        return mat;
    }
}